package orest.front.expert;

 import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.Iterator;

import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
 import orest.common.dao.ExpertDao;
 import orest.common.dao.ExpertKjcgDao;
 import orest.common.dao.ExpertJdcgDao;
 import orest.common.response.Response;
 import orest.common.valueobject.ExpertVal;
 import orest.common.valueobject.ExpertKjcgVal;
import orest.common.util.*;

 public class ExpertAction
{
 public String findExpertByContion(String sql, int pageNo, int pageRecords, long ttl)
{
              Response rp = new Response();
              ExpertDao obj = new ExpertDao();
              StringBuffer result = new StringBuffer();
              boolean opReturn = true;
              String temp = "";
              long total = 0L;
              RecordSet rset = new RecordSet();
              try
              {
                if ((pageNo <= 1) || ((pageNo > 1) && (ttl == 0L)))
                   total = obj.countExpertByCondition(sql, rp);
                else
                   total = ttl;
                opReturn = obj.findExpertByCondition(rp, sql, pageNo, pageRecords, (int)total);
                if (!opReturn)
                   return null;
               }
              catch (Exception e)
               {
                 e.printStackTrace();
               }
              result.append("<total>");
              result.append(total);
              result.append("</total>");
              ArrayList al = rp.getValObjs();
              Iterator iterator = al.iterator();
              try
               {              
                  while (iterator.hasNext())
                {
                  Record record = new Record(); 
                  ExpertVal objElem = (ExpertVal)iterator.next();
                  result.append("<record>");
                  result.append("<ExpertID>");
                   result.append(objElem.getExpertID());
                   record.addData("expertid",objElem.getExpertID());
                  result.append("</ExpertID>");
                  result.append("<Item_180>");
                  result.append(objElem.getItem_180());
                  record.addData("item_180",objElem.getItem_180());
                  result.append("</Item_180>");
                  result.append("<UnitID>");
                  result.append(objElem.getUnitID());
                  record.addData("unitid",objElem.getUnitID());
                  result.append("</UnitID>");
                  result.append("<Item_185>");
                  result.append(objElem.getItem_185());
                  record.addData("item_185",objElem.getItem_185());
                  result.append("</Item_185>");
                  result.append("<WorkSpeciality>");
                  result.append(objElem.getWorkSpeciality());
                  record.addData("workspeciality",objElem.getWorkSpeciality());
                  result.append("</WorkSpeciality>");
                  result.append("<ExpIntro_s>");
                  //result.append(objElem.getExpIntro_s());
                  record.addData("expintro_s",objElem.getExpIntro());
                  result.append("</ExpIntro_s>");
                  result.append("<Photo>");
                  result.append(objElem.getPhoto());
                  record.addData("photo",objElem.getPhoto());
                  result.append("</Photo>");
                  result.append("</record>");
                  record.addData("total",total);
                  objElem = null;
                  rset.add(record);
                }
               }
              catch (Exception e)
              {
                System.out.println("when encoding the string, exception occured.");
                System.out.println("The class is ExpertAction and the function is findKjcgByConditon");
              }
                  al = null;
               obj = null;
               rp = null;
               //return result.toString();
               return rset.getJsonString();
               }
              
 public String findTopExpertByCondition(String sql, String num,String sorttype)
 {
              Response rp = new Response();
              ExpertDao obj = new ExpertDao();
              StringBuffer result = new StringBuffer();
              boolean opReturn = true;
                          
              try
              {
                opReturn = obj.findTopExpertByCondition(rp, num, sql,sorttype);
                if (!opReturn)
                  return null;
              }
              catch (Exception e)
              {
                e.printStackTrace();
              }
              
              ArrayList al = rp.getValObjs();
              Iterator iterator = al.iterator();
              try
              {
                while (iterator.hasNext())
                {
                  ExpertVal objElem = (ExpertVal)iterator.next();
                  result.append("<record>");
                  result.append("<ExpertID>");
                  result.append(objElem.getExpertID());
                  result.append("</ExpertID>");
                  result.append("<Item_180><![CDATA[");
                  result.append(objElem.getItem_180());
                  result.append("]]></Item_180>");
                  result.append("<WorkSpeciality>");
                  result.append(objElem.getWorkSpeciality());
                  result.append("</WorkSpeciality>");
                  result.append("</record>");
                  objElem = null;
                }
              }
              catch (Exception e)
              {
                System.out.println("when encoding the string, exception occured.");
                System.out.println("The class is ExpertAction and the function is findTopExpertByCondition");
              }
              
              al = null;
              obj = null;
              rp = null;
              return result.toString();
              }
 
 public String findExpertByPrk(String id)
{
              Response rp = new Response();
              ExpertDao obj = new ExpertDao();
              StringBuffer result = new StringBuffer();
              boolean opReturn = true;
              GetOption gpt = new GetOption();
              EnCoding encoding =new EnCoding(); 
              Tools tool = new Tools();
              try
              {
                
                opReturn = obj.findExpertByPrk(rp,id);
                if (!opReturn)
                   return null;
               }
              catch (Exception e)
               {
                 e.printStackTrace();
               }
               
              ArrayList al = rp.getValObjs();
              
              Iterator iterator = al.iterator();
              Record record = new Record();
              try
               {              
                  while (iterator.hasNext())
                {
                  ExpertVal objElem = (ExpertVal)iterator.next();
                  result.append ("<record>");
                  
                  result.append("<ExpertID>" );
                  result.append (objElem.getExpertID ());
                  record.addData("expertid", objElem.getExpertID ());
                  result.append ("</ExpertID>");
                  
                  
                  result.append ("<Item_180>");
                  result.append (objElem.getItem_180 ());
                  record.addData("item_180", objElem.getItem_180());
                  result.append ("</Item_180>");
                  
                  result.append ("<Item_185>");
                  result.append (objElem.getItem_185());
                  record.addData("item_185", objElem.getItem_185());
                  result.append ("</Item_185>");
                  
                  result.append ("<UnitID>");
                  result.append (objElem.getUnitID());
                  record.addData("unitid", objElem.getUnitID());
                  result.append ("</UnitID>");
                  
                  result.append ("<Item_181>");
                  result.append (objElem.getItem_181());
                  record.addData("item_181", objElem.getItem_181());
                  result.append ("</Item_181>");
                  
                  
                  result.append ("<Item_183>");
                  result.append (gpt.getXxmcFromXxdm("item_183",objElem.getItem_183()));
                  record.addData("item_183", gpt.getXxmcFromXxdm("item_183",objElem.getItem_183()));
                  result.append ("</Item_183>");
                  
                  result.append ("<Item_184>");
                  result.append (gpt.getXxmcFromXxdm("item_184",objElem.getItem_184()));
                  record.addData("item_184", gpt.getXxmcFromXxdm("item_184",objElem.getItem_184()));
                  result.append ("</Item_184>");
                  
                  result.append ("<Item_182>");
                  result.append (objElem.getItem_182 ());
                  record.addData("item_182", objElem.getItem_182());
                  result.append ("</Item_182>");
                  
                  result.append ("<WorkSpeciality>");
                  result.append (objElem.getWorkSpeciality ());
                  record.addData("workspeciality", objElem.getWorkSpeciality());
                  result.append ("</WorkSpeciality>");
                  
                  
                  result.append ("<GraduateSchool>");
                  result.append (objElem.getGraduateSchool ());
                  record.addData("graduateschool", objElem.getGraduateSchool());
                  result.append ("</GraduateSchool>");
                  
                  result.append ("<Speciality>");
                  result.append (objElem.getSpeciality ());
                  record.addData("speciality", objElem.getSpeciality());
                  result.append ("</Speciality>");
                  
                  
                  result.append ("<Degree>");
                  result.append (objElem.getDegree ());
                  record.addData("degree", objElem.getDegree());
                  result.append ("</Degree>");
                  
                  
                  result.append ("<Clan>");
                  result.append (objElem.getClan());
                  record.addData("clan", objElem.getClan());
                  result.append ("</Clan>");
                  
                  
                  result.append ("<Nationality>");
                  result.append (objElem.getNationality ());
                  record.addData("nationality", objElem.getNationality());
                  result.append ("</Nationality>");
                  
                  
                  result.append ("<Duty>");
                  result.append (objElem.getDuty ());
                  record.addData("duty", objElem.getDuty());
                  result.append ("</Duty>");
                  
                  
                  result.append ("<Nationality>");
                  result.append (objElem.getNationality ());
                  record.addData("nationality", objElem.getNationality());
                  result.append ("</Nationality>");
                  
                  
                  result.append ("<Item_37>");
                  result.append (objElem.getItem_37 ());
                  record.addData("item_37", objElem.getItem_37());
                  result.append ("</Item_37>");
                  
                  
                  result.append ("<Item_38>");
                  result.append (objElem.getItem_38 ());
                  record.addData("item_38", objElem.getItem_38());
                  result.append ("</Item_38>");
                  
                  
                  result.append ("<Item_39>");
                  result.append (objElem.getItem_39 ());
                  record.addData("item_39", objElem.getItem_39());
                  result.append ("</Item_39>");
                  
                  
                  result.append ("<Item_40>");
                  result.append (objElem.getItem_40 ());
                  record.addData("item_40", objElem.getItem_40());
                  result.append ("</Item_40>");
                  
                  
                  result.append ("<Email>");
                  result.append (objElem.getEmail ());
                  record.addData("email", objElem.getEmail());
                  result.append ("</Email>");
                  
                  
                  result.append ("<ExpIntro>");
                  result.append ("<![CDATA["+encoding.xslEncoder(tool.parseNILL(objElem.getExpIntro()))+"]]>");
                  record.addData("expintro", objElem.getExpIntro());
                  result.append ("</ExpIntro>");
                  
                  
                  result.append ("<Achievement>");
                  result.append ("<![CDATA["+encoding.xslEncoder(tool.parseNILL(objElem.getAchievement()))+"]]>");
                  record.addData("achievement", objElem.getAchievement());
                  result.append ("</Achievement>");
                  
                  
                  result.append ("<StrongTrait>");
                  result.append ("<![CDATA["+encoding.xslEncoder(tool.parseNILL(objElem.getStrongTrait()))+"]]>");
                  record.addData("strongtrait", objElem.getStrongTrait());
                  result.append ("</StrongTrait>");
                  
                  
                  result.append ("<NewEnergyClass1>");
                  result.append (objElem.getNewEnergyClass1());
                  record.addData("newenergyclass1", objElem.getNewEnergyClass1());
                  result.append ("</NewEnergyClass1>");
                  
                  
                  result.append ("<NewEnergyClass2>");
                  result.append (objElem.getNewEnergyClass2());
                  record.addData("newenergyclass2", objElem.getNewEnergyClass2());
                  result.append ("</NewEnergyClass2>");
                  
                  
                  
               //   if (findTopKjcgAllByExpID(id,num) != null){
                 //     result.append (findTopKjcgAllByExpID(id,num));
                  //}
                  
                //  if (findTopJdcgAllByExpID(id,num) != null){
                  //    result.append (findTopJdcgAllByExpID(id,num));
                 // }
                  
                  result.append ("</record>");
                  objElem = null;
                  
                }
              }
              catch (Exception e)
              {
                System.out.println("when encoding the string, exception occured.");
                System.out.println("The class is ExpertAction and the function is findExpertByPrk");
              }
                  al = null;
               obj = null;
               rp = null;
               //return result.toString();
               return record.getJsonString();
}
 
 public String findTopKjcgAllByExpID(String expid, String num)
 {
              Response rp = new Response();
              ExpertKjcgDao obj = new ExpertKjcgDao();
              StringBuffer result = new StringBuffer();
              boolean opReturn = true;
              EnCoding encoding =new EnCoding(); 
              Tools tool = new Tools();          
              try
              {
                opReturn = obj.findTopKjcgAllByExpID(rp,expid,num);
                if (!opReturn)
                  return null;
              }
              catch (Exception e)
              {
                e.printStackTrace();
              }
              
              ArrayList al = rp.getValObjs();
             
              Iterator iterator = al.iterator();
              try
              {
                while (iterator.hasNext())
                {
                    ExpertKjcgVal objElem = (ExpertKjcgVal)iterator.next();
                    result.append ("<KjcgID>");
                    result.append (objElem.getKjcgID ());
                    result.append ("</KjcgID>");
                    result.append ("<Item_3>");
                    result.append (objElem.getItem_3 ());
                    result.append ("</Item_3>");
                    result.append ("<Item_186>");
                    result.append (objElem.getItem_186());
                    result.append ("</Item_186>");
                    result.append ("<Item_2>");
                    result.append (objElem.getItem_2 ());
                    result.append ("</Item_2>");
                    result.append ("<Item_126>");
                    result.append (objElem.getItem_126 ());
                    result.append ("</Item_126>");
                    result.append ("<Item_Year>");
                    result.append (objElem.getItem_Year ());
                    result.append ("</Item_Year>");
                    result.append ("<Item_13_1s>");
                    result.append ( "<![CDATA["+encoding.xslEncoder(tool.parseNILL(objElem.getItem_13_1s ()+"]]>")));
                    result.append ("</Item_13_1s>");
                    result.append ("<Item_9>");
                    result.append (objElem.getItem_9 ());
                    result.append ("</Item_9>");
                    result.append ("<CoCreator>");
                    result.append (objElem.getCoCreator ());
                    result.append ("</CoCreator>");
                    objElem = null;
                }
              }
              catch (Exception e)
              {
                System.out.println("when encoding the string, exception occured.");
                System.out.println("The class is ExpertAction and the function is findTopKjcgAllByExpID");
              }
              
              al = null;
              obj = null;
              rp = null;
              return result.toString();
             }
 
 public String findTopJdcgAllByExpID(String expid, String num)
 {
              Response rp = new Response();
              ExpertJdcgDao obj = new ExpertJdcgDao();
              StringBuffer result = new StringBuffer();
              boolean opReturn = true;
              EnCoding encoding =new EnCoding(); 
              Tools tool = new Tools(); 
              try
              {
                opReturn = obj.findTopJdcgAllByExpID(rp,expid,num);
                if (!opReturn)
                  return null;
              }
              catch (Exception e)
              {
                e.printStackTrace();
              }
              
              ArrayList al = rp.getValObjs();
              Iterator iterator = al.iterator();
              try
              {
                while (iterator.hasNext())
                {
                    ExpertKjcgVal objElem = (ExpertKjcgVal)iterator.next();
                    result.append ("<JdcgID>");
                    result.append (objElem.getKjcgID ());
                    result.append ("</JdcgID>");
                    result.append ("<Jdcg_Item_3>");
                    result.append (objElem.getItem_3 ());
                    result.append ("</Jdcg_Item_3>");
                    result.append ("<Item_191>");
                    result.append (objElem.getItem_186());
                    result.append ("</Item_191>");
                    result.append ("<Jdcg_Item_2>");
                    result.append (objElem.getItem_2 ());
                    result.append ("</Jdcg_Item_2>");
                    result.append ("<Jdcg_Item_126>");
                    result.append (objElem.getItem_126 ());
                    result.append ("</Jdcg_Item_126>");
                    result.append ("<Jdcg_Item_Year>");
                    result.append (objElem.getItem_Year ());
                    result.append ("</Jdcg_Item_Year>");
                    result.append ("<Jdcg_Item_13_1s>");
                    result.append ( "<![CDATA["+encoding.xslEncoder(tool.parseNILL(objElem.getItem_13_1s ()+"]]>")));
                    result.append ("</Jdcg_Item_13_1s>");
                    result.append ("<Jdcg_Item_9>");
                    result.append (objElem.getItem_9 ());
                    result.append ("</Jdcg_Item_9>");
                    result.append ("<Appraiser>");
                    result.append (objElem.getCoCreator ());
                    result.append ("</Appraiser>");
                    objElem = null;
                }
              }
              catch (Exception e)
              {
                System.out.println("when encoding the string, exception occured.");
                System.out.println("The class is ExpertAction and the function is findTopJdcgAllByExpID");
              }
              
              al = null;
              obj = null;
              rp = null;
              return result.toString();
              }
 
 }